#!/bin/bash

source /home/shell/util.sh

function update () {
    git -C /usr/local/src/go-probe/ pull
}


# 安装函数
function build () {
    cd /usr/local/src/go-probe/cmd/gprobe
    go build
    cp -r /usr/local/src/go-probe/cmd/gprobe/gprobe /opt/deploy/
}

# 配置函数
function start() {
    cd /opt/deploy
    /opt/nodejs/bin/node start
}

# 是否运行
function isrun () {
    content=$(curl -sLH "Content-Type: application/json" -X POST   --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}'  http://127.0.0.1:40040)
    if [[ -n "${content}" ]]; then
        echo ${content}
    else
        echo "not run"
    fi
}

# 更新脚本
function update_script () {
    if [ -d "/opt/frcdeploy/" ]; then
        rm -rf /opt/frcdeploy/
    fi
  
    cd /opt/
    rz
    if [ ! -f "/opt/frcdeploy.zip" ]; then
        printFail "frcdeploy.zip未上传，停止执行。"
        exit 0;
    fi
    
    unzip ./frcdeploy.zip
    rm -rf /opt/frcdeploy.zip
    cd /opt/frcdeploy/
    npm i
    cp -r /usr/local/src/go-probe/cmd/gprobe/gprobe ./
    read -p "请输入密码[879655737]：" passwd
    passwd=${passwd:-"879655737"}
    read -p "请输入绑定IP地址[${host_addr}]：" ipaddr
    ipaddr=${ipaddr:-"${host_addr}"}
    sed -ri "s/879655737/"${passwd}"/g" /opt/frcdeploy/const.json
    sed -ri "s/\"ip\" : \"127.0.0.1\"/\"ip\" : \""${ipaddr}"\"/g" /opt/frcdeploy/const.json

    ps aux|grep gprobe|grep -v grep|awk '{print $2}'|xargs kill -9

    firewall-cmd --zone=public --add-port=30000-30020/tcp --permanent

    firewall-addport 30040
    firewall-addport 40040

    node init.js prod
    node start.js

    chmod 777 initQuery.sh
    chmod 777 startQuery.sh
    ./initQuery.sh
    ./startQuery.sh
}


function rebuild () {
    printInfo "更新代码"
    git -C /usr/local/src/go-probe pull
    cd /usr/local/src/go-probe/cmd/gprobe
    printInfo "重新编译"
    go build
    cp -r /usr/local/src/go-probe/cmd/gprobe/gprobe /opt/deploy/
    pid=`ps aux|grep gprobe|grep datadir|awk '{print $2}'`;
    echo "杀进程$pid"
    kill -15 $(pidof gprobe)
    cd /opt/deploy/
    /opt/nodejs/bin/node start
    tail -200f /opt/deploy/nodes/node1/gprobe.log
}


$1